

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>mindspore.ops.operations.image_ops &mdash; MindSpore master documentation</title>
  

  
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />

  
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../../../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script src="../../../../_static/jquery.js"></script>
        <script src="../../../../_static/underscore.js"></script>
        <script src="../../../../_static/doctools.js"></script>
        <script src="../../../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../index.html" class="icon icon-home"> MindSpore
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">MindSpore Python API</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.html">mindspore</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.common.initializer.html">mindspore.common.initializer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.communication.html">mindspore.communication</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.compression.html">mindspore.compression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.context.html">mindspore.context</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.html">mindspore.dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.audio.html">mindspore.dataset.audio</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.config.html">mindspore.dataset.config</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.text.html">mindspore.dataset.text</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.transforms.html">mindspore.dataset.transforms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.vision.html">mindspore.dataset.vision</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.mindrecord.html">mindspore.mindrecord</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.html">mindspore.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.probability.html">mindspore.nn.probability</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.transformer.html">mindspore.nn.transformer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.numpy.html">mindspore.numpy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.ops.html">mindspore.ops</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.parallel.html">mindspore.parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.parallel.nn.html">mindspore.parallel.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.profiler.html">mindspore.profiler</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.scipy.html">mindspore.scipy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.train.html">mindspore.train</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.boost.html">mindspore.boost</a></li>
</ul>
<p class="caption"><span class="caption-text">MindSpore C++ API</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://www.mindspore.cn/lite/api/zh-CN/master/api_cpp/mindspore.html">MindSpore Lite↗</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">MindSpore</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../../../index.html">Module code</a> &raquo;</li>
        
      <li>mindspore.ops.operations.image_ops</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for mindspore.ops.operations.image_ops</h1><div class="highlight"><pre>
<span></span><span class="c1"># Copyright 2020-2021 Huawei Technologies Co., Ltd</span>
<span class="c1">#</span>
<span class="c1"># Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1"># you may not use this file except in compliance with the License.</span>
<span class="c1"># You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1"># http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>
<span class="c1"># ============================================================================</span>

<span class="sd">&quot;&quot;&quot;image_ops&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">...</span> <span class="kn">import</span> <span class="n">context</span>
<span class="kn">from</span> <span class="nn">..._checkparam</span> <span class="kn">import</span> <span class="n">Validator</span> <span class="k">as</span> <span class="n">validator</span>
<span class="kn">from</span> <span class="nn">..._checkparam</span> <span class="kn">import</span> <span class="n">Rel</span>
<span class="kn">from</span> <span class="nn">...common</span> <span class="kn">import</span> <span class="n">dtype</span> <span class="k">as</span> <span class="n">mstype</span>
<span class="kn">from</span> <span class="nn">..primitive</span> <span class="kn">import</span> <span class="n">PrimitiveWithInfer</span><span class="p">,</span> <span class="n">prim_attr_register</span><span class="p">,</span> <span class="n">Primitive</span>


<div class="viewcode-block" id="CropAndResize"><a class="viewcode-back" href="../../../../api_python/ops/mindspore.ops.CropAndResize.html#mindspore.ops.CropAndResize">[docs]</a><span class="k">class</span> <span class="nc">CropAndResize</span><span class="p">(</span><span class="n">PrimitiveWithInfer</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Extracts crops from the input image tensor and resizes them.</span>

<span class="sd">    Note:</span>
<span class="sd">        In case that the output shape depends on crop_size, the crop_size must be constant.</span>

<span class="sd">    Args:</span>
<span class="sd">        method (str): An optional string that specifies the sampling method for resizing.</span>
<span class="sd">            It can be &quot;bilinear&quot;, &quot;nearest&quot; or &quot;bilinear_v2&quot;. The option &quot;bilinear&quot; stands for standard bilinear</span>
<span class="sd">            interpolation algorithm, while &quot;bilinear_v2&quot; may result in better result in some cases. Default: &quot;bilinear&quot;</span>
<span class="sd">        extrapolation_value (float): An optional float value used extrapolation, if applicable. Default: 0.0.</span>

<span class="sd">    Inputs:</span>
<span class="sd">        - **x** (Tensor) - The input image must be a 4-D tensor of shape [batch, image_height, image_width, depth].</span>
<span class="sd">          Types allowed: int8, int16, int32, int64, float16, float32, float64, uint8, uint16.</span>
<span class="sd">        - **boxes** (Tensor) - A 2-D tensor of shape [num_boxes, 4].</span>
<span class="sd">          The i-th row of the tensor specifies the coordinates of a box in the box_ind[i] image</span>
<span class="sd">          and is specified in normalized coordinates [y1, x1, y2, x2]. A normalized coordinate value of y is mapped to</span>
<span class="sd">          the image coordinate at y * (image_height - 1), so as the [0, 1] interval of normalized image height is</span>
<span class="sd">          mapped to [0, image_height - 1] in image height coordinates. We do allow y1 &gt; y2, in which case the sampled</span>
<span class="sd">          crop is an up-down flipped version of the original image. The width dimension is treated similarly.</span>
<span class="sd">          Normalized coordinates outside the [0, 1] range are allowed, in which case we use extrapolation_value to</span>
<span class="sd">          extrapolate the input image values. Types allowed: float32.</span>
<span class="sd">        - **box_index** (Tensor) - A 1-D tensor of shape [num_boxes] with int32 values in [0, batch).</span>
<span class="sd">          The value of box_ind[i] specifies the image that the i-th box refers to. Types allowed: int32.</span>
<span class="sd">        - **crop_size** (Tuple[int]) - A tuple of two int32 elements: (crop_height, crop_width).</span>
<span class="sd">          Only constant value is allowed. All cropped image patches are resized to this size.</span>
<span class="sd">          The aspect ratio of the image content is not preserved. Both crop_height and crop_width need to be positive.</span>
<span class="sd">    Outputs:</span>
<span class="sd">        A 4-D tensor of shape [num_boxes, crop_height, crop_width, depth] with type: float32.</span>

<span class="sd">    Raises:</span>
<span class="sd">        TypeError: If `method` is not a str.</span>
<span class="sd">        TypeError: If `extrapolation_value` is not a float.</span>
<span class="sd">        ValueError: If `method` is not one of &#39;bilinear&#39;, &#39;nearest&#39;, &#39;bilinear_v2&#39;.</span>

<span class="sd">    Supported Platforms:</span>
<span class="sd">        ``Ascend`` ``GPU`` ``CPU``</span>

<span class="sd">    Examples:</span>
<span class="sd">        &gt;&gt;&gt; class CropAndResizeNet(nn.Cell):</span>
<span class="sd">        ...     def __init__(self, crop_size):</span>
<span class="sd">        ...         super(CropAndResizeNet, self).__init__()</span>
<span class="sd">        ...         self.crop_and_resize = ops.CropAndResize()</span>
<span class="sd">        ...         self.crop_size = crop_size</span>
<span class="sd">        ...</span>
<span class="sd">        ...     def construct(self, x, boxes, box_index):</span>
<span class="sd">        ...         return self.crop_and_resize(x, boxes, box_index, self.crop_size)</span>
<span class="sd">        ...</span>
<span class="sd">        &gt;&gt;&gt; BATCH_SIZE = 1</span>
<span class="sd">        &gt;&gt;&gt; NUM_BOXES = 5</span>
<span class="sd">        &gt;&gt;&gt; IMAGE_HEIGHT = 256</span>
<span class="sd">        &gt;&gt;&gt; IMAGE_WIDTH = 256</span>
<span class="sd">        &gt;&gt;&gt; CHANNELS = 3</span>
<span class="sd">        &gt;&gt;&gt; image = np.random.normal(size=[BATCH_SIZE, IMAGE_HEIGHT, IMAGE_WIDTH, CHANNELS]).astype(np.float32)</span>
<span class="sd">        &gt;&gt;&gt; boxes = np.random.uniform(size=[NUM_BOXES, 4]).astype(np.float32)</span>
<span class="sd">        &gt;&gt;&gt; box_index = np.random.uniform(size=[NUM_BOXES], low=0, high=BATCH_SIZE).astype(np.int32)</span>
<span class="sd">        &gt;&gt;&gt; crop_size = (24, 24)</span>
<span class="sd">        &gt;&gt;&gt; crop_and_resize = CropAndResizeNet(crop_size=crop_size)</span>
<span class="sd">        &gt;&gt;&gt; output = crop_and_resize(Tensor(image), Tensor(boxes), Tensor(box_index))</span>
<span class="sd">        &gt;&gt;&gt; print(output.shape)</span>
<span class="sd">        (5, 24, 24, 3)</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="nd">@prim_attr_register</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s2">&quot;bilinear&quot;</span><span class="p">,</span> <span class="n">extrapolation_value</span><span class="o">=</span><span class="mf">0.0</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Initialize CropAndResize&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">init_prim_io_names</span><span class="p">(</span><span class="n">inputs</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="s1">&#39;boxes&#39;</span><span class="p">,</span> <span class="s1">&#39;box_index&#39;</span><span class="p">,</span> <span class="s1">&#39;crop_size&#39;</span><span class="p">],</span> <span class="n">outputs</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;y&#39;</span><span class="p">])</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check_value_type</span><span class="p">(</span><span class="s2">&quot;method&quot;</span><span class="p">,</span> <span class="n">method</span><span class="p">,</span> <span class="p">[</span><span class="nb">str</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check_string</span><span class="p">(</span><span class="n">method</span><span class="p">,</span> <span class="p">[</span><span class="s2">&quot;bilinear&quot;</span><span class="p">,</span> <span class="s2">&quot;nearest&quot;</span><span class="p">,</span> <span class="s2">&quot;bilinear_v2&quot;</span><span class="p">],</span> <span class="s2">&quot;method&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">method</span> <span class="o">=</span> <span class="n">method</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check_value_type</span><span class="p">(</span><span class="s2">&quot;extrapolation_value&quot;</span><span class="p">,</span> <span class="n">extrapolation_value</span><span class="p">,</span> <span class="p">[</span><span class="nb">float</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">extrapolation_value</span> <span class="o">=</span> <span class="n">extrapolation_value</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">is_ge</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">get_context</span><span class="p">(</span><span class="s2">&quot;enable_ge&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__infer__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">boxes</span><span class="p">,</span> <span class="n">box_index</span><span class="p">,</span> <span class="n">crop_size</span><span class="p">):</span>
        <span class="c1"># get shape</span>
        <span class="n">x_shape</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="s1">&#39;shape&#39;</span><span class="p">])</span>
        <span class="n">boxes_shape</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">boxes</span><span class="p">[</span><span class="s1">&#39;shape&#39;</span><span class="p">])</span>
        <span class="n">box_index_shape</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">box_index</span><span class="p">[</span><span class="s1">&#39;shape&#39;</span><span class="p">])</span>
        <span class="c1"># get value</span>
        <span class="k">if</span> <span class="n">crop_size</span><span class="p">[</span><span class="s1">&#39;value&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;For &#39;</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="si">}</span><span class="s2">&#39;, the &#39;crop_size&#39; cannot be None, but got </span><span class="si">{</span><span class="n">crop_size</span><span class="p">[</span><span class="s1">&#39;value&#39;</span><span class="p">]</span><span class="si">}</span><span class="s2">.&quot;</span><span class="p">)</span>
        <span class="n">crop_size_value</span> <span class="o">=</span> <span class="n">crop_size</span><span class="p">[</span><span class="s1">&#39;value&#39;</span><span class="p">]</span>
        <span class="c1"># get dtype</span>
        <span class="n">x_dtype</span> <span class="o">=</span> <span class="n">x</span><span class="p">[</span><span class="s1">&#39;dtype&#39;</span><span class="p">]</span>
        <span class="n">boxes_dtype</span> <span class="o">=</span> <span class="n">boxes</span><span class="p">[</span><span class="s1">&#39;dtype&#39;</span><span class="p">]</span>
        <span class="n">box_index_dtype</span> <span class="o">=</span> <span class="n">box_index</span><span class="p">[</span><span class="s1">&#39;dtype&#39;</span><span class="p">]</span>
        <span class="n">crop_size_dtype</span> <span class="o">=</span> <span class="n">crop_size</span><span class="p">[</span><span class="s1">&#39;dtype&#39;</span><span class="p">]</span>
        <span class="c1"># check dytpe</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check_tensor_dtype_valid</span><span class="p">(</span><span class="s2">&quot;x&quot;</span><span class="p">,</span> <span class="n">x_dtype</span><span class="p">,</span>
                                           <span class="p">[</span><span class="n">mstype</span><span class="o">.</span><span class="n">int8</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">int16</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">int32</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">int64</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">float16</span><span class="p">,</span>
                                            <span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">float64</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">uint8</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">uint16</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check_tensor_dtype_valid</span><span class="p">(</span><span class="s2">&quot;boxes&quot;</span><span class="p">,</span> <span class="n">boxes_dtype</span><span class="p">,</span> <span class="p">[</span><span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check_tensor_dtype_valid</span><span class="p">(</span><span class="s2">&quot;box_index&quot;</span><span class="p">,</span> <span class="n">box_index_dtype</span><span class="p">,</span> <span class="p">[</span><span class="n">mstype</span><span class="o">.</span><span class="n">int32</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check_value_type</span><span class="p">(</span><span class="s2">&quot;crop_size&quot;</span><span class="p">,</span> <span class="n">crop_size_value</span><span class="p">,</span> <span class="p">[</span><span class="nb">tuple</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="c1"># check input shape rank</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="s2">&quot;x rank&quot;</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">x_shape</span><span class="p">),</span> <span class="s2">&quot;expected&quot;</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="n">Rel</span><span class="o">.</span><span class="n">EQ</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="s2">&quot;boxes rank&quot;</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">boxes_shape</span><span class="p">),</span> <span class="s2">&quot;expected&quot;</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">Rel</span><span class="o">.</span><span class="n">EQ</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="s2">&quot;box_index rank&quot;</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">box_index_shape</span><span class="p">),</span> <span class="s2">&quot;expected&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">Rel</span><span class="o">.</span><span class="n">EQ</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="s2">&quot;crop_size size&quot;</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">crop_size_value</span><span class="p">),</span> <span class="s2">&quot;expected&quot;</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">Rel</span><span class="o">.</span><span class="n">EQ</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="s2">&quot;boxes dim_0&quot;</span><span class="p">,</span> <span class="n">boxes_shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="s2">&quot;box_index dim_0&quot;</span><span class="p">,</span> <span class="n">box_index_shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">Rel</span><span class="o">.</span><span class="n">EQ</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="s2">&quot;boxes dim_1&quot;</span><span class="p">,</span> <span class="n">boxes_shape</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="s2">&quot;expected&quot;</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="n">Rel</span><span class="o">.</span><span class="n">EQ</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="c1"># check crop_size_value</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="s2">&quot;crop_height&quot;</span><span class="p">,</span> <span class="n">crop_size_value</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="s2">&quot;minimum&quot;</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">Rel</span><span class="o">.</span><span class="n">GT</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="s2">&quot;crop_width&quot;</span><span class="p">,</span> <span class="n">crop_size_value</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="s2">&quot;minimum&quot;</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">Rel</span><span class="o">.</span><span class="n">GT</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="c1"># check crop_size element type</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="s2">&quot;crop_height dtype&quot;</span><span class="p">,</span> <span class="n">crop_size_dtype</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="s2">&quot;expected&quot;</span><span class="p">,</span> <span class="p">[</span><span class="n">mstype</span><span class="o">.</span><span class="n">int32</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">int64</span><span class="p">],</span> <span class="n">Rel</span><span class="o">.</span><span class="n">IN</span><span class="p">,</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="n">validator</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="s2">&quot;crop_width dtype&quot;</span><span class="p">,</span> <span class="n">crop_size_dtype</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="s2">&quot;expected&quot;</span><span class="p">,</span> <span class="p">[</span><span class="n">mstype</span><span class="o">.</span><span class="n">int32</span><span class="p">,</span> <span class="n">mstype</span><span class="o">.</span><span class="n">int64</span><span class="p">],</span> <span class="n">Rel</span><span class="o">.</span><span class="n">IN</span><span class="p">,</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>

        <span class="n">num_boxes</span> <span class="o">=</span> <span class="n">boxes_shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">crop_height</span> <span class="o">=</span> <span class="n">crop_size_value</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">crop_width</span> <span class="o">=</span> <span class="n">crop_size_value</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">depth</span> <span class="o">=</span> <span class="n">x_shape</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>
        <span class="n">out_shape</span> <span class="o">=</span> <span class="p">(</span><span class="n">num_boxes</span><span class="p">,</span> <span class="n">crop_height</span><span class="p">,</span> <span class="n">crop_width</span><span class="p">,</span> <span class="n">depth</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_ge</span><span class="p">:</span>
            <span class="n">out_shape</span> <span class="o">=</span> <span class="p">(</span><span class="n">num_boxes</span><span class="p">,</span> <span class="n">x_shape</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">crop_height</span><span class="p">,</span> <span class="n">crop_width</span><span class="p">)</span>
        <span class="k">return</span> <span class="p">{</span><span class="s1">&#39;shape&#39;</span><span class="p">:</span> <span class="n">out_shape</span><span class="p">,</span>
                <span class="s1">&#39;dtype&#39;</span><span class="p">:</span> <span class="n">mstype</span><span class="o">.</span><span class="n">float32</span><span class="p">,</span>
                <span class="s1">&#39;value&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">}</span></div>

<span class="k">class</span> <span class="nc">NonMaxSuppressionV3</span><span class="p">(</span><span class="n">Primitive</span><span class="p">):</span>
    <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Greedily selects a subset of bounding boxes in descending order of score.</span>

<span class="sd">    .. warning::</span>
<span class="sd">        When input &quot;max_output_size&quot; is negative, it will be treated as 0.</span>

<span class="sd">    Note:</span>
<span class="sd">        This algorithm is agnostic to where the origin is in the coordinate system.</span>
<span class="sd">        This algorithm is invariant to orthogonal transformations and translations of the coordinate system;</span>
<span class="sd">        thus translating or reflections of the coordinate system result in the same boxes being</span>
<span class="sd">        selected by the algorithm.</span>

<span class="sd">    Inputs:</span>
<span class="sd">        - **boxes** (Tensor) - A 2-D Tensor of shape [num_boxes, 4].</span>
<span class="sd">        - **scores** (Tensor) - A 1-D Tensor of shape [num_boxes] representing a single score</span>
<span class="sd">          corresponding to each box (each row of boxes), the num_boxes of &quot;scores&quot; must be equal to</span>
<span class="sd">          the num_boxes of &quot;boxes&quot;.</span>
<span class="sd">        - **max_output_size** (Union[Tensor, Number.Int]) - A scalar integer Tensor representing the maximum</span>
<span class="sd">          number of boxes to be selected by non max suppression.</span>
<span class="sd">        - **iou_threshold** (Union[Tensor, Number.Float]) - A 0-D float tensor representing the threshold for</span>
<span class="sd">          deciding whether boxes overlap too much with respect to IOU, and iou_threshold must be equal or greater</span>
<span class="sd">          than 0 and be equal or smaller than 1.</span>
<span class="sd">        - **score_threshold** (Union[Tensor, Number.Float]) - A 0-D float tensor representing the threshold for</span>
<span class="sd">          deciding when to remove boxes based on score.</span>

<span class="sd">    Outputs:</span>
<span class="sd">        A 1-D integer Tensor of shape [M] representing the selected indices from the boxes tensor,</span>
<span class="sd">        where M &lt;= max_output_size.</span>

<span class="sd">    Raises:</span>
<span class="sd">        TypeError: If the dtype of `boxes` and `scores` is different.</span>
<span class="sd">        TypeError: If the dtype of `iou_threshold` and `score_threshold` is different.</span>
<span class="sd">        TypeError: If `boxes` is not tensor or its dtype is not float16 or float32.</span>
<span class="sd">        TypeEroor: If `scores` is not tensor or its dtype is not float16 or float32.</span>
<span class="sd">        TypeError: If `max_output_size` is not tensor or scalar.If `max_output_size` is not int32 or int64.</span>
<span class="sd">        TypeError: If `iou_threshold` is not tensor or scalar. If its type is not float16 or float32.</span>
<span class="sd">        TypeError: If `score_threshold` is not tensor or scalar. If its type is not float16 or float32.</span>
<span class="sd">        ValueError: If the size of shape of `boxes` is not 2 or the second value of its shape is not 4.</span>
<span class="sd">        ValueError: If the size of shape of `scores` is not 1.</span>
<span class="sd">        ValueError: If each of the size of shape of `max_output_size`, `iou_threshold`, `score_threshold` is not 0.</span>

<span class="sd">    Supported Platforms:</span>
<span class="sd">        ``Ascend``</span>

<span class="sd">    Examples:</span>
<span class="sd">        &gt;&gt;&gt; boxes = Tensor(np.array([[1, 2, 3, 4], [1, 3, 3, 4], [1, 3, 4, 4],</span>
<span class="sd">        ...                          [1, 1, 4, 4], [1, 1, 3, 4]]), mstype.float32)</span>
<span class="sd">        &gt;&gt;&gt; scores = Tensor(np.array([0.4, 0.5, 0.72, 0.9, 0.45]), mstype.float32)</span>
<span class="sd">        &gt;&gt;&gt; max_output_size = Tensor(5, mstype.int32)</span>
<span class="sd">        &gt;&gt;&gt; iou_threshold = Tensor(0.5, mstype.float32)</span>
<span class="sd">        &gt;&gt;&gt; score_threshold = Tensor(0, mstype.float32)</span>
<span class="sd">        &gt;&gt;&gt; nonmaxsuppression = ops.NonMaxSuppressionV3()</span>
<span class="sd">        &gt;&gt;&gt; output = nonmaxsuppression(boxes, scores, max_output_size, iou_threshold, score_threshold)</span>
<span class="sd">        &gt;&gt;&gt; print(output)</span>
<span class="sd">        [3 2 0]</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="nd">@prim_attr_register</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Initialize NonMaxSuppressionV3&quot;&quot;&quot;</span>
</pre></div>

           </div>
           
          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2021, MindSpore.

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>